<script setup lang="ts">
import type { FormSchema } from '@fl/origin-components/Form'
import { orgAppKey } from '@fl/utils/appKey'
import { useResetPwd } from '@fl/api/iam-api'
import { resetPwd } from '@fl/api/iam-api'
import { useForm } from '@fl/hooks/web/useForm'
import { useValidator } from '@fl/hooks/web/useValidator'
import { Form } from '@fl/origin-components/Form'
import { useUserStore } from '@fl/store/modules/user'
import { ElDivider, ElMessage } from 'element-plus'
import JSEncrypt from 'jsencrypt'
import { reactive, ref } from 'vue'
import { updatePassword } from '@fl/api/iam-api'
import { encrypt } from '@fl/utils/SM4'
import { PublicKey } from './index'

const { required } = useValidator()
const userStore = useUserStore()

const isEnterprise = ref<boolean>(false) // true运营端、false其他端
isEnterprise.value = import.meta.env.VITE_IS_PLATFORM

const formSchema = reactive<FormSchema[]>([
    {
        colProps: {
            span: 24,
        },
        component: 'InputPassword',
        componentProps: {
            strength: true,
        },
        field: 'oldPassword',
        label: '旧密码',
    },
    {
        colProps: {
            span: 24,
        },
        component: 'InputPassword',
        componentProps: {
            strength: true,
        },
        field: 'newPassword',
        label: '新密码',
    },
    {
        colProps: {
            span: 24,
        },
        component: 'InputPassword',
        componentProps: {
            strength: true,
        },
        field: 'confirmPassword',
        label: '确认新密码',
    },
])

const rules = reactive({
    oldPassword: [
        required(),
    ],
    newPassword: [
        required(),
        // {
        //     asyncValidator: async (_, val, callback) => {
        //         const formData = await getFormData()
        //         const { confirmPassword } = formData
        //         if (val !== confirmPassword) {
        //             callback(new Error('新密码与确认新密码不一致'))
        //         }
        //         else {
        //             callback()
        //         }
        //     },
        // },
    ],
    confirmPassword: [
        required(),
        {
            asyncValidator: async (_, val, callback) => {
                const formData = await getFormData()
                const { newPassword } = formData
                if (val !== newPassword) {
                    callback(new Error('确认新密码与新密码不一致'))
                }
                else {
                    callback()
                }
            },
        },
    ],
    password: [required()],
})

const { formMethods, formRegister } = useForm()
const { getElFormExpose, getFormData } = formMethods

const saveLoading = ref(false)
async function save() {
    const elForm = await getElFormExpose()
    const valid = await elForm?.validate().catch((err) => {
        console.log(err)
    })
    if (valid) {
        const formInfo = await getFormData()
            updatePassword({
                oldPassword	: encrypt(formInfo?.oldPassword),
                newPassword: encrypt(formInfo?.newPassword),
                appKey: orgAppKey[window.location.host]
            }).then(() => {
                ElMessage.success('操作成功')
            })
    }
}
</script>

<template>
    <Form :rules="rules"
          :schema="formSchema"
          @register="formRegister"
    />

    <ElDivider />

    <BaseButton type="primary"
                :loading="saveLoading"
                @click="save"
    >
        确认修改
    </BaseButton>
</template>
